﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using log4net;
using StockFinder.Model;
using StockFinder.StockLists.Exchanges;
using StockFinder.YahooFinance;

namespace Testharness
{
    public partial class YahooFinanceTestForm : Form
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(YahooFinanceTestForm)); 

        public YahooFinanceTestForm()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            var csvExtractor = new YahooFinanceCsvStockPriceHistoryExtractor();
            var prices = csvExtractor.GetHistoricPrices(TestCommon.COMMON_DIRECTORY + "chuy_yahoofinance.csv");

            foreach (var dailyPrice in prices)
            {
                
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            var downloader = new YahooFinanceOnlineStockPriceHistoryDownloader();

            var symbol = new Symbol {Name = "NTI"};

            var prices = downloader.DownloadSymbolPricesCsv(symbol, DateTime.Today.AddYears(-40), DateTime.Today);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            var downloader = new YahooFinanceOnlineStockPriceHistoryDownloader();

            var symbol = new Symbol { Name = "NTI" };

            const string DIRECTORY = @"C:\SVNHome\trunk\howzat117\Common\YahooFinanceOfflinePriceHistories";

            downloader.DownloadAndSaveSymbolPricesCsv(symbol, DateTime.Today.AddYears(-40), DateTime.Today, DIRECTORY);
        }

        private void button4_Click(object sender, EventArgs e)
        {
            _BackgroundWorker.RunWorkerAsync();
        }

        private void button5_Click(object sender, EventArgs e)
        {

        }

        private void _BackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            const string DIRECTORY = @"C:\NasdaqOnlineAllYahooFinancePriceHistories";
            var allNasdaqOnline = new NasdaqOnlineAllYahooFinanceOnlinePricesStockList() { LoadPrices = false };
            //var allNasdaqOnline = new NasdaqOnlineAllAmexNasdaqNyseYahooFinancePriceLoaderStockList() { LoadPrices = false };
            var downloader = new YahooFinanceOnlineStockPriceHistoryDownloader();
            var stocksCount = allNasdaqOnline.Count;
            int counter = 0;

            foreach (var symbol in allNasdaqOnline)
            {
                ++counter;

                downloader.DownloadAndSaveSymbolPricesCsv(
                    symbol, DateTime.Today.AddYears(-40), DateTime.Today, DIRECTORY);

                var progress = ((double)counter / (double)stocksCount) * 100;

               _BackgroundWorker.ReportProgress(Convert.ToInt32(progress));
            }
        }

        private void _BackgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void _BackgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {

        }
    }
}
